#include"seqlist.h"

//初始化
void SeqListInit(SL* ps)
{
	assert(ps);

	ps->a = (SLDataType*)malloc(sizeof(SLDataType) * 4);
	if (ps->a == NULL)
	{
		perror("malloc falied");
		exit(-1);
	}

	ps->capacity = 4;
	ps->size = 0;

}
//容量检测
void SeqListcapacity(SL* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		ps->a = (SLDataType*)realloc(ps->a, sizeof(SLDataType) * (ps->capacity * 2));
		if (ps->a == NULL)
		{
			perror("realloc falied");
			exit(-1);
		}
		ps->capacity *= 2;
	}
}

//头插
void SLFrontPush(SL* ps, SLDataType x)
{
	assert(ps);
	SeqListcapacity(ps);

	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end] = ps->a[end + 1];
		end--;
	}

	ps->size++;
}

//头删
void SLFrontBack(SL* ps)
{
	assert(ps);
	assert(ps->size > 0);
	int begin = 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
		begin++;
	}
	ps->size--;
}

//尾插
void SLPopPush(SL* ps, SLDataType x)
{
	assert(ps);
	SeqListcapacity(ps);
	ps->a[ps->size] = x;
	ps->size++;
}

//尾删
void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size > 0);
	ps->size--;
}

//指定下标（pos）位置插入元素x（SLInsert）
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SeqListcapacity(ps);
	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}

//删除下标（pos）位置的元素
void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);

	int begin = pos + 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
		begin++;
	}
	ps->size--;

}

//打印
void print(SL* ps)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");

}
